package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.AssessDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Evaluate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;

@Service(interfaceClass = AssessService.class)
@Transactional
public class AssessServiceImpl implements AssessService {

    @Autowired
    private AssessDao assessDao;


    //分页查询
    @Override
    public PageResult findPage(QueryPageBean pageBean) {
        Integer currentPage = pageBean.getCurrentPage();
        Integer pageSize = pageBean.getPageSize();
        String queryString = pageBean.getQueryString();

        //如果条件不为空和空字符串 那么查询的页数为1
        if (queryString != null && !"".equals(queryString)) {
            currentPage = 1;
        }
        //使用分页助手插件实现分页查询
        PageHelper.startPage(currentPage, pageSize);
        Page<Evaluate> page = assessDao.findByCondition(queryString);
        return new PageResult(page.getTotal(), page.getResult());
    }

    //新增
    @Override
    public void add(Evaluate evaluate) {
        //添加日期
        String date = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        evaluate.setTime(date);
        assessDao.add(evaluate);
    }

    @Override
    public void delete(Integer id) {
        assessDao.deleteById(id);
    }

    @Override
    public void edit(Evaluate evaluate) {
        assessDao.edit(evaluate);
    }

    @Override
    public Evaluate findById(Integer id) {
        return assessDao.findById(id);
    }
}
